package com.yhx.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.yhx.iservice.ICheckitemService;
import com.yhx.mapper.CheckitemMapper;
import com.yhx.pojo.Checkitem;
import com.yhx.utils.MessageConstant;
import com.yhx.utils.PageResult;
import com.yhx.utils.QueryPageBean;
import com.yhx.utils.Result;
import org.apache.dubbo.config.annotation.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
@Service
public class CheckitemServiceImpl implements ICheckitemService {
    @Resource
    private CheckitemMapper checkitemMapper;
    @Override
    public List<Checkitem> list() {
        try {
            List<Checkitem> list = checkitemMapper.selectList(null);
            return list;
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }

    @Override
    public PageResult pagelist(QueryPageBean queryPageBean) {
        //创建一个条件构造器
        QueryWrapper<Checkitem> queryWrapper = new QueryWrapper<>();
        //查看前端查询条件
        String queryString = queryPageBean.getQueryString();
        if (queryString!=null&&queryString.length()>0){
            queryWrapper.like("code", queryString).or().like("name",queryString);
        }
        //创建一个分页对象
        Page<Checkitem> page = new Page<>(queryPageBean.getCurrentPage(), queryPageBean.getPageSize());
        //查询
        Page<Checkitem> checkitemPage = checkitemMapper.selectPage(page, queryWrapper);
        //返回查询结果
        return new PageResult(checkitemPage.getTotal(), checkitemPage.getRecords());
    }

    @Override
    public int save(Checkitem checkitem) {
        return checkitemMapper.insert(checkitem);
    }

    @Override
    public int delete(int id) {
        return checkitemMapper.deleteById(id);
    }

    @Override
    public int update(Checkitem checkitem) {
        return checkitemMapper.updateById(checkitem);
    }

    @Override
    public int deleteAll(Integer[] ids) {
        List<Integer> list = Arrays.asList(ids);
        return checkitemMapper.deleteBatchIds(list);
    }

    @Override
    public Checkitem getByid(int id) {
        return checkitemMapper.selectById(id);
    }

    @Override
    public Result saveAll(List<Checkitem> list) {
        if(list==null || list.size()==0){
            return new Result(false, "请在excel表格中添加数据再上传");
        }
        ArrayList<Integer> list1 = new ArrayList<>();
        for (Checkitem checkitem : list) {
            list1.add(checkitem.getId());
        }
        //查找id是否存在
        //从数据库中找出来的数据
        List<Checkitem> checkitemList = checkitemMapper.selectBatchIds(list1);
        if(checkitemList!=null ||checkitemList.size()>0){
            for (Checkitem checkitem : checkitemList) {
                Checkitem checkitem1 = getCheckitem(list, checkitem);
                if(checkitem1!=null){
                    checkitemMapper.updateById(checkitem1);
                }
            }
            list.removeAll(checkitemList);
        }
        for (Checkitem checkitem : list) {
            if(checkitem!=null){
                checkitemMapper.insert(checkitem);
            }
        }
        return new Result(true, MessageConstant.UPLOAD_SUCCESS);
    }

    public Checkitem getCheckitem(List<Checkitem> list, Checkitem checkitem){
        for (Checkitem checkitem1 : list) {
            if(checkitem1!=checkitem){
                return checkitem1;
            }
        }
        return null;
    }
}
